package KK.Type
{ 
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	public class Bytes extends ByteArray
	{
		
		//占据大小
		static public const S_INT16:int=0x2;//16大小
		static public const S_INT32:int=0x4;//32大小
		static public const S_BYTE:int=0x1;//bytes大小
		
		
		//最前一个 byte代表符号位
		static public const TNUMBER:int=0x10000000;
		static public const TINT:int=0x20000000;
		static public const TBYTES:int=0x30000000;
		static public const TSTRING:int=0x40000000; 
		static public const TUINT:int=0x50000000;
		
		static public const UINT16:int=TUINT|0x02;//无符号16int
		static public const INT16:int=TINT|0x2;//16位int
		static public const INT32:int=TUINT|0x4;//32位无符号INT
		static public const UINT32:int=TINT|0x4;//32位int
		static public const DOUBLE:int=TNUMBER|0x8;//64位double
		static public const STRING:int=TSTRING;//字符串
		static public const BYTES:int=TBYTES;//bytes
		
		private var sizeVector:Vector.<int>;
		
		
		public function Bytes()
		{
			//TODO: implement function
			super();
			init();
		}
		private function init():void{ 
			endian=Endian.LITTLE_ENDIAN;
		}
		//写入规则
		protected function WriteVector(sizeVector:Vector.<int>):void{
			this.sizeVector=sizeVector;
			
		}
		//写入规则里的第position个字节
		protected function Write(data:*,pos:int):void{
			this.writeVector(data,pos); 
		}
		
		//写入规则里的第position个字节
		protected function writeVector(data:*,pos:int):void{
			var i:int;
			var count:int=0;
			this.position=0;
			const size:int=sizeVector[pos];
			const wType:int=size&0xf0000000;
			
			while(i<pos){
				count+=(this.sizeVector[i]&0xFFFFFFF);
				i++;
			} 
			this.position=count;
			
			
			switch(true){
				case size==UINT16:
				case size==INT16:
					this.position=count;
					this.writeShort(int(data));
					break;
				case size==UINT32:
					this.position=count;
					this.writeUnsignedInt(int(data));
					break;
				case size==INT32:
					this.position=count;
					this.writeInt(int(data));
					break;
				case size==DOUBLE:
					this.position=count;
					this.writeShort(Number(data));
					break;
				case STRING==wType:
					this.position=count;
					this.writeMultiByte(String(data)+String.fromCharCode(0),'utf-8');
					//					this[count+(byteSize^STRING)-1]=this[data.length]=0;
					break;
				case BYTES==wType: 
					this.position=count;
					if(data){
						this.writeBytes(data,0,data.length);
					}
					
					break;
			}
		}
		
		//读取规则里的第position个字节
		protected function Read(pos:int):*{
			
			var result:*=this.readVector(pos); 
			return result;
		}
		//读取规则里的第position个字节
		protected function readVector(pos:int):*{
			var result:*;
			var count:int=0;
			var i:int=0;
			
			const size:int=this.sizeVector[pos];
			const rType:int=0xF0000000&size;
			
			while(i<pos){
				count+=(this.sizeVector[i]&0xFFFFFFF);//去除符号位
				i++;
			}
			this.position=count;
			
			switch(true){
				case INT16==size:
					this.position=count;
					result=this.readShort();
					break;
				case UINT16==size:
					this.position=count;
					result=this.readUnsignedShort();
					break;
				case INT32==size:
					this.position=count;
					result=this.readUnsignedInt();
					break;
				case UINT32==size:
					this.position=count;
					result=this.readInt();
					break;
				case DOUBLE==size:
					this.position=count;
					result=this.readDouble();
					break;
				case STRING==rType:
					position=count;
					result=this.readMultiByte(size^STRING,'utf-8');
					break;
				case BYTES==rType: 
					result=new ByteArray();
					result.endian=this.endian;
					position=count;
					this.readBytes(result,0,(size^BYTES));
					break;
			}
			return result;
		}
		
		//获取读取规则
		protected function GetSequence():Vector.<int>{
			var result:Vector.<int>=this.sizeVector;
			return result;
		}
		//设置读取规则
		protected function SetSequence(sizeVector:Vector.<int>):void{
			this.sizeVector=sizeVector;
			this[Size-1]=0;
			
		}
		//获取结构体大小
		public function get Size():int{
			var result:int=0;
			var i:int,j:int=this.sizeVector.length;
			for(i=0;i<j;i++){
				result+=(sizeVector[i]&0xFFFFFFF);//去除符号位
			}
			return result;
		}
		
		
		public function get bytes():ByteArray{
			return this;
		}
		 
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		/*
		
		public function clear():void{
			this.c_Byte.clear();
		}
		
		
		public function writeBytes(bytes:ByteArray,offset:uint,length:uint):void{
			this.c_Byte.writeBytes(bytes,offset,length);
		}
		
		public function get length():uint{
			return this.c_Byte.length;
		}
		
		public function get bytes():ByteArray{
			return this.c_Byte;
		}
		public function set bytes(value:ByteArray):void{
			this.c_Byte=value;
		}
		
		public function get position():uint{
			return this.c_Byte.position
		}
		public function set position(value:uint):void{
			this.c_Byte.position=value;
		}
		public function readBytes(bytes:ByteArray,offset:uint,length:uint):void{
			this.c_Byte.readBytes(bytes,offset,length);
		}
		
		public function get bytesAvailable():uint{
			return this.c_Byte.bytesAvailable;
		}
		*/
	}
}